package com.yanqu.road.server.cmd.player;

import com.yanqu.road.entity.task.args.union.UnionActivityTaskArgs;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.entity.config.player.HouseInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.player.UserHouse;
import com.yanqu.road.entity.task.args.union.UnionActivityTaskArgs;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.house.HouseProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.HouseModule;
import com.yanqu.road.server.manger.config.HouseMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;

@Cmd(code = Protocol.S_CLICK_HOUSE, desc = "乞丐点金")
public class ClickHouseCmd extends AbstractCommand {

	private int clickTimes;

	private BigInteger silver;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		HouseProto.HouseClickReqMsg reqMsg = HouseProto.HouseClickReqMsg.parseFrom(packet.getBytes());
		clickTimes = reqMsg.getClickTimes();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if (clickTimes < 0) {// 传来的为负数直接返回
			clickTimes = 0;
		}

		UserHouse userHouse = player.getModule(HouseModule.class).getUserHouse();

		// 判断是否超过每秒最多次数
		long nowTime = System.currentTimeMillis();
		long seconds = nowTime - userHouse.getLastClickTime();
		if (seconds < GameConfig.BANK_CLICK_INTERVAL_SECOND) {
			clickTimes = 0;
			return;
		}
		//校验点击次数
		if (clickTimes > GameConfig.BANK_SECOND_CLICK_MAX_TIMES) {
			clickTimes = GameConfig.BANK_SECOND_CLICK_MAX_TIMES;
		}

		HouseInfo houseInfo = HouseMgr.getHouseInfo(player.getUserInfo().getHouseLv());
		silver = houseInfo.getClickAddSilver().multiply(BigInteger.valueOf(clickTimes));

		//设置最近点击时间
		userHouse.setTotalClickTimes(userHouse.getTotalClickTimes() + clickTimes);
		userHouse.setLastClickTime(nowTime);
		//增加银两
		player.getModule(CurrencyModule.class).addCurrency(new Property(GameConfig.GAME_MONEY_SILVER, silver), eLogMoneyType.System, eLogMoneyType.ClickHouse);
		//成就、任务
		player.notifyListener(eGamePlayerEventType.HouseClickTimes.getValue(), clickTimes);
		player.notifyListener(eGamePlayerEventType.UnionHouseClickTimes.getValue(), new UnionActivityTaskArgs(UnionActivityTaskArgs.TYPE_ADD_VALUE, clickTimes, player.getUserInfo().getUnionUid()));
		player.notifyListener(eGamePlayerEventType.HouseTotalClickTimes.getValue(), userHouse);
	}

	@Override
	public void sendData(GamePlayer player) {
		HouseProto.HouseClickRespMsg.Builder respMsg = HouseProto.HouseClickRespMsg.newBuilder();
		respMsg.setRet(ret);
		if(null != silver) {
			respMsg.setSilver(silver.toString());
		}else {
			respMsg.setSilver("0");
		}
		player.sendPacket(Protocol.U_CLICK_HOUSE, respMsg);
	}
}
